﻿/*
* Copyright (c) 2022,
* All rights reserved.
*
* 文件名称：pet.cpp
* 创建日期：2022/3/12
* 摘 要：宠物类接口实现
*
* 当前版本：1.0
* 作 者：mason
* 完成日期：
*/

#include "pet.h"
#include "ui_pet.h"

Pet::Pet(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Pet)
{
    ui->setupUi(this);

    /* 窗口属性设置 */
    setAutoFillBackground(false);
    setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_TranslucentBackground,true);

    /* 屏幕中央显示 */
    QDesktopWidget *deskdop = QApplication::desktop();
    move((deskdop->width() - this->width()) / 2, \
         (deskdop->height() - this->height()) / 2);

    /* pixmap list init */
    for (int i = 0; i < PIXMAP_NUM_START; i++)
    {
        QString fileName = QString(AN_PATH_START).arg(i + 1);
        QPixmap map(fileName);
        pixmap_list.append(map);
    }
    pixmap_num = PIXMAP_NUM_START;
    pixmap_index = 0;
    is_playing = false;
    current_timer = startTimer(300);
    ant_mode = ANT_MODE_START;


    //信息窗初始化
    info_widget = new ArrowWidget;
    info_widget->move(this->geometry().x() + (this->width() / 2),\
             this->geometry().y() - 80);
    //w1->adjustSize();


    //鼠标悬停
    this->setAttribute(Qt::WA_Hover, true); //开启悬停事件
    this->installEventFilter(this); //安装事件过滤器

    setCursor(Qt::ClosedHandCursor); //设置鼠标样式
}

bool Pet::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::HoverEnter)
    {
        if (is_playing)
        {
            info_widget->setText(music_ctl->getCurrentMusicInfo());

        } else {
            info_widget->setText(HI_TEXT);

        }
        info_widget->show();
    } else if(QEvent::HoverLeave == event->type()) {
        info_widget->hide();
    }
}

void Pet::timerEvent(QTimerEvent *event)
{
    pixmap_index++;
    if (pixmap_index >= pixmap_num)
    {
        pixmap_index = 0;
        switch (ant_mode) {
        case ANT_MODE_START:
            //killTimer(current_timer);
            //current_timer = -1;
            antPlay(PIXMAP_NUM_DAILY, AN_PATH_DAILY, ANT_MODE_DAILY);
            break;
        default:
            break;
        }



    }
    else
    {
        repaint();  //窗口重绘
    }
    //pixmap_index %= MAX_NUM_ANI_FRAMES;

}

void Pet::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    pixmap_list[pixmap_index].scaled(this->width(),this->height(), \
                                     Qt::KeepAspectRatio);
    painter.drawPixmap(this->rect(), pixmap_list.at(pixmap_index), \
                       pixmap_list.at(pixmap_index).rect());
}

void Pet::mousePressEvent(QMouseEvent *e)
{
    //仅对鼠标左键响应，右键留着使用菜单功能
    if (e->button() == Qt::LeftButton)
    {

        pos = e->globalPos();
    } else {
        QMenu menu;
        menu.setCursor(Qt::ClosedHandCursor); //设置鼠标样式
        //添加菜单项，指定图标、名称、响应函数
        menu.addAction(QIcon(":/icon/resource/icon/music.png"), "播放",this,SLOT(slotPlayMusic()));
        menu.addAction(QIcon(":/icon/resource/icon/next.png"), "下一曲",this,SLOT(slotMusicNext()));
        menu.addAction(QIcon(":/icon/resource/icon/add.png"), "添加",this,SLOT(slotMusicAdd()));

        if (is_playing)
        {
            menu.actions()[0]->setText("停止");
        }

        //在鼠标位置显示
        menu.exec(QCursor::pos());

    }
}

void Pet::slotMusicAdd()
{
    if(!is_playing)
    {
        music_ctl = new PetMusic;
        music_ctl->playMusic();
        is_playing = true;
    }

    bool isOK;
    QString text = QInputDialog::getText(this, "添加音乐",
                                                       "输入音乐名",
                                                       QLineEdit::Normal,
                                                       "",
                                                       &isOK);
    if(isOK) {

        music_ctl->sortMusic(text);
    }


}

void Pet::slotMusicNext()
{
    if (is_playing)
    {
        music_ctl->musicNext();
    }
}


void Pet::antPlay(int pix_nums, QString path, ANT_MODE mode)
{
    //killTimer(current_timer);
    //清空缓存帧
    pixmap_list.clear();
    pixmap_num = pix_nums;

    //加载动画帧
    for (int i = 0; i < pix_nums; i++)
    {
        QString fileName = QString(path).arg(i + 1);
        QPixmap map(fileName);
        pixmap_list.append(map);
    }
    pixmap_num = pix_nums;
    pixmap_index = 0;
    ant_mode = mode;
    if (-1 == current_timer)
    {
        current_timer = startTimer(300);
    }
}

void Pet::antPlayMusic()
{
//    //清空缓存帧
//    pixmap_list.clear();
//    pixmap_num = 0;

//    //加载动画帧
//    for (int i = 0; i < PIXMAP_NUM_PLAY_MUSIC; i++)
//    {
//        QString fileName = QString(AN_PATH_PLAY_MUSIC).arg(i + 1);
//        QPixmap map(fileName);
//        pixmap_list.append(map);
//    }
//    pixmap_num = PIXMAP_NUM_PLAY_MUSIC;
//    pixmap_index = 0;
//    ant_mode = ANT_MODE_MUSIC;
//    current_timer = startTimer(300);

}

void Pet::slotPlayMusic()
{
    if(!is_playing)
    {
        music_ctl = new PetMusic;
        music_ctl->playMusic();
        is_playing = true;

        /* 开始动画 */
        antPlay(PIXMAP_NUM_PLAY_MUSIC, AN_PATH_PLAY_MUSIC, ANT_MODE_MUSIC);

    }
    else
    {
        delete music_ctl;
        music_ctl = nullptr;
        is_playing = false;
        antPlay(PIXMAP_NUM_DAILY, AN_PATH_DAILY, ANT_MODE_DAILY);
    }

}

void Pet::mouseMoveEvent(QMouseEvent *e)
{
    //仅对鼠标左键响应，右键留着使用菜单功能
    if (!e->buttons().testFlag(Qt::LeftButton))
            return;
    int dx = e->globalX() - pos.x();
    int dy = e->globalY() - pos.y();
    pos = e->globalPos();
    move(x() + dx, y() + dy);
    qDebug()<< this->geometry().x() + this->width();

    //info_widget->myMove(this->geometry().x() + this->width() , this->geometry().y());
#ifdef Q_OS_WIN32
    info_widget->move(this->geometry().x() + (this->width() / 2),\
             this->geometry().y() - 80);
#endif
}


/* 起始动画播放 */
//void Pet::animationStart()
//{


//}

Pet::~Pet()
{
    delete ui;
}
